<?php
class Catalogue extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model(array("Msection","Mcatalogue"));
		$this->breadcrumbs->push('Catalogue', 'admincp/catalogue/view');
	}
	public function view(){
        $data['icon_page']=$this->config->item('icon_view');
        $data['title']='Catalogue Manager';
        $data['sub_title']='Catalogue Manager';
        $data['columns'] = array(
        	'a.id' => trans('id'),
        	'b.title as sec_title' => trans('section'),
        	'a.title as cata_title' => trans('title'),
        	'a.created_at' => trans('created_at')
        );
        $data['catalogues']=$this->Mcatalogue->listCatalogue(array_keys($data['columns']));
		$this->parser->parse("catalogue/view.tpl", NULL, $data);
	}

	public function add(){
		$data['icon_page']=$this->config->item('icon_create');
        $data['title']='Create New Catalogue';
        $data['sub_title']='Create New Catalogue';
		$data['section']=$this->Msection->list_all();
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$title = $this->input->post('cata_name');
			$data_insert = array(
				'title' => $this->input->post('cata_name'),
				'url' => url_title(convert_accented_characters($title), '-', TRUE),
				'sec_id' => $this->input->post('section'),
				'created_at' => time(), 
			);
			$this->Mcatalogue->insert($data_insert);
			$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
			redirect(admincp_url()."catalogue/view");
		}
		$this->breadcrumbs->push('Create Catalogue', 'admincp/catalogue/add');
		$this->parser->parse("catalogue/add.tpl", NULL, $data);
	}

	public function edit(){
		$cache_id = $id = $this->uri->segment(4);
		$data['icon_page']=$this->config->item('icon_edit');
        $data['title']='Edit Catalogue';
        $data['sub_title']='Edit Catalogue';
		$data['section']=$this->Msection->list_all();
		$data['info']=$this->Mcatalogue->get_data($id);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$title = $this->input->post('cata_name');
			$data_update = array(
				'title' => $this->input->post('cata_name'),
				'url' => url_title(convert_accented_characters($title), '-', TRUE),
				'sec_id' => $this->input->post('section'),
				'updated_at' => time(), 
			);
			$this->Mcatalogue->update($data_update,$id);
			$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
			redirect(admincp_url()."catalogue/view");
		}
		$this->breadcrumbs->push('Edit Catalogue', 'admincp/catalogue/edit/'.$id);
		$this->parser->parse("catalogue/edit.tpl", $cache_id, $data);
	}

	public function delete(){
		$id = $this->uri->segment(4);
		$this->Mcatalogue->delete($id);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(admincp_url()."catalogue/view");
	}
}